package com.itchu.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.Authority;
import com.itchu.system.domain.SysRole;
import com.itchu.system.mapper.AuthorityMapper;
import com.itchu.system.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class AuthorityServiceImpl implements AuthorityService {

    @Autowired
    private AuthorityMapper authorityMapper;

    /**
     * 根据权限的父id查找出父级权限
     * @param pid
     * @return
     */
    @Override
    public List<Authority> showAuthorityByPid(Long pid) {
        List<Authority> list = authorityMapper.showAuthorityByPid(pid);
        return list;
    }

    /**
     * 查找权限的分页信息
     * @param key   搜索条件
     * @param page  当前页
     * @param rows  页大小
     * @param sortBy
     * @param desc
     * @return
     */
    @Override
    public PageResult<Authority> findByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //开启分页
        PageHelper.startPage(page,rows);
        //创建一个封装条件的对象
        Example example = new Example(Authority.class);
        if (!StringUtils.isEmpty(key)) {
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("name","%" + key + "%");
            criteria.orLike("id","%" + key + "%");
        }
        if (!StringUtils.isEmpty(sortBy)) {
            example.setOrderByClause(sortBy+" "+(desc ? "desc" : "asc"));
        }
        List<Authority> authoritys = authorityMapper.selectByExample(example);
        PageInfo<Authority> pageInfo = new PageInfo<>(authoritys);
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getPages(),authoritys);
    }

    /**
     * 保存权限
     * @param authority
     * @return
     */
    @Override
    public void addAuthority(Authority authority) {
        if (authority != null) {
            authorityMapper.insertSelective(authority);
        }
    }

    /**
     * 修改权限
     * @param authority
     * @return
     */
    @Override
    public void updateAuthority(Authority authority) {
        if (authority != null) {
            authorityMapper.updateByPrimaryKey(authority);
        }
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @Override
    public void deletAuthority(Long id) {
        authorityMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据权限id查找权限
     * @param id
     * @return
     */
    @Override
    public Authority findById(Long id) {
        Authority authority = authorityMapper.selectByPrimaryKey(id);
        return authority;
    }

    @Override
    public List<Authority> AllAuthority() {
        List<Authority> authorities = authorityMapper.selectAll();
        return authorities;
    }

    /**
     * 查找出所有权限
     * 你可能会有这样的疑问：上面不是查找出权限的分页了吗，为什么还要查一遍
     * 因为这个是树形组件要用到，所以要查出所有
     * @return
     */
    @Override
    public List<Authority> findAuthorityById(Long id) {
        List<Authority> list  = authorityMapper.findAuthorityById(id);
        return list;
    }

    /**
     * 根据请求路径获取用户角色
     * @param requestUrl
     * @return
     */
    @Override
    public List<SysRole> findRoleByRequestURL(String requestUrl) {
        if (requestUrl == "") {
            return null;
        }

        List<SysRole> roles = authorityMapper.findRoleByRequestURL(requestUrl);
        return roles;
    }
}
